package com.hfkz.project.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{

	private static Logger logger = Logger.getLogger(BaseAction.class);
	/**
	 * 
	 */
	private String result;
	private static final long serialVersionUID = -2058749350589417219L;
	
	/**
	 * 获取Cookie=>JSESSIONID
	 * @return
	 * @author hanmin
	 * @createDate 2014-7-29
	 */
	
	
	public Map getCookies()
	{
		Map rep = new HashMap();
		Cookie cookies[] = ServletActionContext.getRequest().getCookies();
		
		for(Cookie c :cookies){
            if("JSESSIONID".equals(c.getName()))
            {
            	rep.put("JSESSIONID", c.getValue());
            }
        }
		return rep;
	}
	
	@Override
	public String execute() throws Exception {
		this.result = "dsdddd";
		return SUCCESS;
	}

	/**
	 * 给Session中赋值
	 * @param key
	 * @param value
	 */
	public void setAttr(String key,String value){
		ActionContext.getContext().getSession().put(key, value);
	}
	
	/**
	 * 给Session中赋值
	 * @param key
	 * @param value
	 */
	public void setAttrByObj(String key,Object value){
		ActionContext.getContext().getSession().put(key, value);
	}
	
	public String a(){
		
		return "c";
	}
	
	/**
	 * 移除request中的属性
	 * @param name
	 */
	public void removeAttr(String key) {
		ActionContext.getContext().getSession().remove(key);
	}
	
	/**
	 * 返回request中的值（字符串），为空或不存在返回null
	 * @param name
	 * @return
	 */
	public String getPara(String key,String value){
		return ActionContext.getContext().getSession().put(key, value).toString();
	}
	
	
	/**设置分页
	 * @param total 总记录数
	 * @param number 每页条数
	 * @param pageCount 总页数
	 */
	public int setSumPage(int total,int number,int pageCount){
          if(total<number){
        	  pageCount=1;
          }else{
        	  //总记录数和每页条数取莫
        	  int temp=total%number;
        	  if(temp>0){
        		  pageCount=(total/number)+1;
        	  }else{
        		  pageCount=(total/number);
        	  }
        	  
          }
          
          return pageCount;
	}
	
	/**设置分页
	 * @param total 总记录数
	 * @param number 每页条数
	 * @param pageCount 总页数
	 */
	public int setSumPage(int total,int number){
		  int pageCount=0;
          if(total<number){
        	  pageCount=1;
          }else{
        	  //总记录数和每页条数取莫
        	  int temp=total%number;
        	  if(temp>0){
        		  pageCount=(total/number)+1;
        	  }else{
        		  pageCount=(total/number);
        	  }
          }
          return pageCount;
	}
	
	public HttpServletResponse getHttpReponse(){
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
	    response.setContentType("text/html;charset=utf-8;");
		return response;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	
}
